#!/usr/bin/python3
# ******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# ******************************************************************************/
import logging


class Logger:
    def __init__(self):
        self._logger = logging.getLogger(self.__class__.__name__)
        self._format = logging.Formatter(
            '%(asctime)s-[%(name)s]-[file:%(filename)s:%(lineno)d]-[function:%(funcName)s]-[%(levelname)s]: %(message)s'
            , datefmt='%a, %d %b %Y %H:%M:%S')
        self._level = "DEBUG"

    def __call__(self, *args, **kwargs):
        self._init_logger()
        return self._logger

    def _init_logger(self):
        self._logger.setLevel(self._level)
        self._add_stream_handler()
        self._add_file_handler()
        return self._logger

    def _add_stream_handler(self):
        stream_handler = logging.StreamHandler()
        stream_handler.setFormatter(self._format)
        stream_handler.setLevel(self._level)
        self._logger.addHandler(stream_handler)

    def _add_file_handler(self):
        file_handler = logging.FileHandler(filename="/var/log/code/code.log")
        file_handler.setFormatter(self._format)
        file_handler.setLevel(self._level)
        self._logger.addHandler(file_handler)


log = Logger()()
